$(function() {

	//页面加载
	loadmyarchives($("#eos").attr("data-type"));
	//完成操作
	$(".confirm-choose").bind("click", confirmChoose);
	//进入编辑
	$("#eos").bind("click", toEdit);
	//新增档案
	$(".addArchives").bind("click", toAdd);
	//查看 档案
	//$(".keepArchives").on("touchstart",toCheck);

});

function loadmyarchives(type) {
	//USERID
	var param = {
		"patientid": USERID
	};
	archivesModule.loadMyArchives(param, showmyarchives);
}

function showmyarchives(data) {
	loadlist(data, $("#eos").attr("data-type"));
}

function deletearchives(id) {
	var btnArray = ['否', '是'];
	mui.confirm('您确定删除该条记录？', '提示', btnArray, function(e) {
		if(e.index == 1) {
			mui.alert("确定", "提示");
		}
	})
}

function loadlist(data, type) {
	console.log(data);
	var showhtml = "";
	if(data.length > 0) {
		$(".user-archivesBot button").css("background", "#47BAFE");
		for(var i = 0; i < data.length; i++) {
			var item = data[i];
			console.log(item);
			showhtml += "<li class=\"keepArchives\"><label for=\"radio" + i + "\">";
			var $sex = item.sex;
			if(item.sex==1){
				$sex ="男";
			}else{
				$sex ="女";
			}
			if(i == 0) {
				showhtml += "<input type=\"radio\" name=\"adNames\" id=\"radio" + i + "\" value=\"" + item.id + "\" info=\"" + item.name + "#" + $sex + "#" + item.age +"#" + item.phoneNumber + "\" class=\"R\"/>";
			} else {
				showhtml += "<input type=\"radio\" name=\"adNames\" id=\"radio" + i + "\" value=\"" + item.id + "\" checked class=\"R\" info=\"" + item.name + "#" + $sex + "#" + item.age +"#" + item.phoneNumber+ "\"/>";
			}
			showhtml += "<span class=\"radio-style\"></span>";
			showhtml += "<img src=\"../../images/docture.png\" />";
			showhtml += "<div class=\"user\">";
			showhtml += "<font class=\"checked-name\">" + item.name + "</font>";
			showhtml += "<p><font class=\"checked-sex\">" + $sex + "</font><font class=\"checked-age\">" + item.age + "</font></p>";
			showhtml += "</div></label></li>";
		}
		$("#eos").show();
		$("#user-archivesList").show();
		$(".user-archivesBot").show();
		$("#user-archivesList").append(showhtml);
		$(".user-archivesList li:first-child").hide();
	} else if(data = undefined || data == null || data.length == 0) {
		$(".user-archivesList li:first-child").show();
	}
}

//提交完成跳转;
//1.入口为快速提问,则跳转到选择医生界面;
//2.如果由医生页面跳转过来,则回调提交支付界面;

var doctorid = JSON.parse(sessionStorage.getItem("doctorid_info_key"));

function confirmChoose() {

	var _archivesId = $("input[name='adNames']:checked").val();
	console.log(_archivesId);
	if(_archivesId == null) {
		mui.alert("请先添加新档案!", "提示");
		return false;
	}

	//获取选中的档案基本信息:姓名+性别+年龄
	var infos = $("input[name='adNames']:checked").attr("info");
	console.log(infos);
	if(infos != null) {
		var advocateNum = {};
		infos = infos.split("#");
		advocateNum.name = infos[0];
		advocateNum.sex = infos[1];
		advocateNum.age = infos[2];
		advocateNum.phone = infos[3];
		advocateNum.archivesId = _archivesId;

		//将选择的咨询人信息存储到sessionStorage中，便于在提问页面获取
		sessionStorage.setItem("archives_info_key", JSON.stringify(advocateNum));
	}

	// 档案选择完成后，根据业务类型，跳转对应的页面【skipType= 1:快速提问, 2图文或电话, 3续方,4精防】
	var skipType = sessionStorage.getItem("skipType");
	if(skipType == 1) {
		location.href = "question-fast.html?archivesid=" + _archivesId; //快速提问
	} else if(skipType == 2) {
		location.href = "../doctor/ask-doctor.html?archivesid=" + _archivesId; //问医生
	} else if(skipType == 3) {
		//在线续方
		location.href = "../onlineContinuation/medication-consultation.html?archivesid=" + _archivesId + "&doctorId=" + doctorid;
	} else if(skipType == 4) {
		location.href = "../../html/electronicArchives/electronic-archives.html?archivesid=" + _archivesId;
	}
}

//跳转修改
function toEdit() {
	var dataLength = $("input[name='adNames']").length;
	if(dataLength < 1) {
		$("#user-archivesList").show();
		$("#user-archivesList li:first-child").show();
		$("#eos").hide();
		$(".user-archivesBot").hide();
		return false;
	}

	var url = "user-archives-edit.html";
	var skipType = sessionStorage.getItem("skipType");
	if(utilModule.checkUndefined(skipType) != "") {
		url += "?skipType=" + skipType;
	}
	location.href = url;
}

//跳转新增
function toAdd() {
	var url = "user-archives-add.html";
	var skipType = sessionStorage.getItem("skipType");
	if(utilModule.checkUndefined(skipType) != "") {
		url += "?skipType=" + skipType;
	}
	location.href = url;
}
